//
//  YWGrabTroubleBillCell.m
//  maintain
//
//  Created by kyo on 16/12/5.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWGrabTroubleBillCell.h"
#import "UILabel+AttributedText.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "UIImage+Extension.h"

#import "YWCommonTool.h"
#import "YWHandler.h"
#import "YWMission.h"
#import "YWBuilding.h"
#import "YWApartment.h"
#import "YWFloor.h"
#import "YWLocation.h"
#import "YWBuildingManager.h"
#import "YWEquipmentManager.h"
#import "YWMissionManager.h"
#import "YWEquipment.h"
#import "YWReportTroubleModel.h"
#import "YWEquipmentType.h"
#import "YYLabel.h"

@interface YWGrabTroubleBillCell()
/** 报障描述 */
@property (nonatomic, strong) YYLabel *noteLabel;
/** 设备类型 */
@property (nonatomic, strong) YYLabel *equipmentLabel;
/** 设备位置 */
@property (nonatomic, strong) YYLabel *locationLabel;
/** 报障编号 */
@property (nonatomic, strong) YYLabel *bianhaoLabel;
/** 报障人 */
@property (nonatomic, strong) YYLabel *reporterLabel;
/** 抢单按钮 */
@property (nonatomic, strong) UIButton *grabBillButton;
@end

@implementation YWGrabTroubleBillCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setReportTrouble:(YWReportTroubleModel *)reportTrouble{
    _reportTrouble = reportTrouble;
    YWHandler *fromUser = nil;
    NSString *location = nil;
    if (_reportTrouble.channel == YWPostTroubleTypeEwinApp) {
        fromUser = _reportTrouble.reporter;
    }else{
        fromUser = [[YWHandler alloc] init];
        fromUser.userName = _reportTrouble.troubleContact.contactName;
        fromUser.mobile = _reportTrouble.troubleContact.contactNo;
    }
    
    if (_reportTrouble.building && _reportTrouble.apartment && _reportTrouble.floor && _reportTrouble.location) {
        location = [NSString stringWithFormat:@"%@%@%@%@",_reportTrouble.building.buildingName?:@"", _reportTrouble.apartment.apartmentName?:@"", _reportTrouble.floor.floorName?:@"", _reportTrouble.location.locationName?:@""];
    }else if (_reportTrouble.equipment.equipmentId.length > 0) {
        YWEquipment* equipment = _reportTrouble.equipment;
        location = [[YWBuildingManager shareManager] equipmentLocation:equipment] ?: NSLocalizedString(@"none", nil);
    }
    if (location.length == 0 && _reportTrouble.location.locationId){
        location = [[YWBuildingManager shareManager] areaLocationWithLocationId:_reportTrouble.location.locationId];
    }
    
    _noteLabel.attributedText = [YWCommonTool attributedTextWithFirstText:NSLocalizedString(@"explainFormatter", nil) firstColor:[UIColor customTextGrayBlackColor] secondText:_reportTrouble.note?:NSLocalizedString(@"none", nil) secondColor:[UIColor blackColor] font:kContentFont];
    
    NSString *equipmentName = nil;
    if (_reportTrouble.equipment) {
        equipmentName = _reportTrouble.equipment.equipmentName?:NSLocalizedString(@"none", nil);//_troubleMission.equipmentName?:([[YWEquipmentManager shareManager] equipmentWithId:_troubleMission.equipmentId].equipmentName?:@"无");
    }else if (_reportTrouble.equipmentType){
        equipmentName = _reportTrouble.equipmentType.equipmentTypeName?:NSLocalizedString(@"none", nil);//[[YWEquipmentManager shareManager] equipmentTypeNameWithTypeId:_troubleMission.equipmentTypeId];
    }
    _equipmentLabel.attributedText = [YWCommonTool attributedTextWithFirstText:NSLocalizedString(@"equipmentFormatter", nil) firstColor:[UIColor customTextGrayBlackColor] secondText:equipmentName.length ? equipmentName : NSLocalizedString(@"none", nil) secondColor:[UIColor customYellowColor] font:kContentFont];
    
    _locationLabel.attributedText = [YWCommonTool attributedTextWithFirstText:NSLocalizedString(@"locationFormatter", nil) firstColor:[UIColor customTextGrayBlackColor] secondText:location.length?location:NSLocalizedString(@"none", nil) secondColor:[UIColor blackColor] font:kContentFont];
    
    NSString *name = fromUser.userName.length?fromUser.userName:NSLocalizedString(@"none", nil);
    
    NSString* reportTimeStr = [YWCommonTool showTimeWithNormalFormatter:_reportTrouble.reportTime / 1000];
    
    _bianhaoLabel.text = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"code", nil),_reportTrouble.troubleSequence?:NSLocalizedString(@"none", nil)];
    _reporterLabel.text = [NSString stringWithFormat:@"%@: %@  %@",NSLocalizedString(@"toubleReporter", nil),name,reportTimeStr];
    
    self.noteLabel.preferredMaxLayoutWidth = self.noteLabel.frame.size.width;
    self.equipmentLabel.preferredMaxLayoutWidth = self.equipmentLabel.frame.size.width;
    self.locationLabel.preferredMaxLayoutWidth = self.locationLabel.frame.size.width;
}

- (void)setUpSubviews {
    
    UIView *superview = self.contentView;
    [self.noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superview.mas_top).offset(6);
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(self.grabBillButton.mas_left).offset(-8);
    }];
    
    [self.equipmentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_noteLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(self.grabBillButton.mas_left).offset(-8);
    }];
    
    [self.locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_equipmentLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(self.grabBillButton.mas_left).offset(-8);
    }];
    
    [self.grabBillButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_locationLabel.mas_centerY);
        make.right.mas_equalTo(superview.mas_right).offset(-8);
        make.size.mas_equalTo(CGSizeMake(kButtonW*3/2, kButtonH));
    }];
    
    [self.bianhaoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_locationLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(superview.mas_left).offset(8);
        make.right.mas_equalTo(self.grabBillButton.mas_left).offset(-8);
    }];
    [self.reporterLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_bianhaoLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(superview.mas_left).offset(8);
        make.right.mas_equalTo(superview.mas_right).offset(-8);
        make.bottom.mas_equalTo(superview.mas_bottom).offset(-8);
    }];
}

//- (void)layoutSubviews{
//    [super layoutSubviews];
//    
//}

- (YYLabel *)bianhaoLabel{
    if (!_bianhaoLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        _bianhaoLabel = label;
        [self.contentView addSubview:_bianhaoLabel];
    }
    return _bianhaoLabel;
}

- (YYLabel *)reporterLabel{
    if (!_reporterLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        _reporterLabel = label;
        [self.contentView addSubview:_reporterLabel];
    }
    return _reporterLabel;
}


- (YYLabel *)equipmentLabel{
    if (!_equipmentLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _equipmentLabel = label;
        [self.contentView addSubview:_equipmentLabel];
    }
    return _equipmentLabel;
}

- (YYLabel *)locationLabel{
    if (!_locationLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _locationLabel = label;
        [self.contentView addSubview:_locationLabel];
    }
    return _locationLabel;
}

- (YYLabel *)noteLabel{
    if (!_noteLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _noteLabel = label;
        [self.contentView addSubview:_noteLabel];
    }
    return _noteLabel;
}

- (UIButton *)grabBillButton{
    if (!_grabBillButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:NSLocalizedString(@"grab", nil) forState:UIControlStateNormal];
        button.titleLabel.font = kContentFont;
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.backgroundColor = [UIColor customGreenColor];
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = 4.0;
        
        [button addTarget:self action:@selector(clickGrabBillButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:button];
        _grabBillButton = button;
    }
    return _grabBillButton;
}

- (void)clickGrabBillButtonEvent:(id)sender{
    if ([self.delegate respondsToSelector:@selector(clickGrabBillEvent:)]) {
        [self.delegate clickGrabBillEvent:self];
    }
}

@end
